package com.example.aixiangshou.runninggame;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.example.zhouwei.library.CustomPopWindow;
import com.x_memteor.base_master.application.MyApplication;
import com.x_memteor.base_master.base.BaseActivity;
import com.x_memteor.base_master.bean.GameBean;
import com.x_memteor.base_master.bean.ItemBean;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * test push。
 */
public class NombreActivity extends BaseActivity implements CompoundButton.OnCheckedChangeListener{
    @BindView(R.id.tb_nombre)
    Toolbar tbNombre;
    @BindView(R.id.textView)
    TextView textView;
    @BindView(R.id.rv_nombre)
    RecyclerView rvNombre;
    @BindView(R.id.imv_sound)
    ImageView imvSound;
    @BindView(R.id.imv_close)
    ImageView imvClose;
    private RecyclerView mRecycler;
    private List<NombreBean> mNombreBeans;
    private NombreAdapter mNombreAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public void initRootView() {
        setContentView(R.layout.activity_nombre);
    }

    @Override
    public void initView() {
        mRecycler = findViewById(R.id.rv_nombre);
    }

    GameBean gameBean;

    @Override
    public void initData() {
        mNombreBeans = new ArrayList<>();
        gameBean = MyApplication.getInstance().getGameBean();
        if (gameBean != null && gameBean.getList() != null) {
            for (int i = 0; i < gameBean.getList().size(); i++) {
                NombreBean nombreBean = new NombreBean();
                ItemBean itemBean = gameBean.getList().get(i);
                nombreBean.mFront = getResources().getDrawable(R.drawable.bg_main1);
                nombreBean.mBack = getResources().getDrawable(itemBean.getIcon());
                nombreBean.mName = itemBean.getIcon();
                mNombreBeans.add(nombreBean);
            }
        }
        mNombreAdapter = new NombreAdapter(R.layout.item_nombre, mNombreBeans);
    }

    @Override
    public void initListener() {
        GridLayoutManager layoutManager = new GridLayoutManager(this, 2);
        mRecycler.setLayoutManager(layoutManager);
        mRecycler.setAdapter(mNombreAdapter);
        mNombreAdapter.openLoadAnimation(BaseQuickAdapter.SLIDEIN_LEFT);
        mNombreAdapter.isFirstOnly(false);
    }

    @OnClick({R.id.imv_sound, R.id.imv_close})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.imv_sound:
                showSoundPow();
                break;
            case R.id.imv_close:
                showClosePow();

                break;
        }
    }

    CustomPopWindow popSoundWindow;
    CustomPopWindow popCloseWindow;

    private void showClosePow() {
        View contentView = LayoutInflater.from(NombreActivity.this).inflate(R.layout.pop_closer, null);
        handleClose(contentView);

        popCloseWindow = new CustomPopWindow.PopupWindowBuilder(NombreActivity.this)
                .setView(contentView)//显示的布局
                .enableBackgroundDark(true) //弹出popWindow时，背景是否变暗
                .setBgDarkAlpha(0.7f) // 控制亮度
                .create()//创建PopupWindow
                .showAtLocation(imvSound.getRootView(), Gravity.CENTER, 0, 0);
    }


    private void showSoundPow() {
        View contentView = LayoutInflater.from(NombreActivity.this).inflate(R.layout.pop_center, null);
        handleSound(contentView);

        popSoundWindow = new CustomPopWindow.PopupWindowBuilder(NombreActivity.this)
                .setView(contentView)//显示的布局
                .enableBackgroundDark(true) //弹出popWindow时，背景是否变暗
                .setBgDarkAlpha(0.7f) // 控制亮度
                .create()//创建PopupWindow
                .showAtLocation(imvSound.getRootView(), Gravity.CENTER, 0, 0);

    }

    /**
     * 处理弹出显示内容、点击事件等逻辑
     *
     * @param contentView
     */
    private void handleSound(View contentView) {
        View.OnClickListener listener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (popSoundWindow != null) {
                    popSoundWindow.dissmiss();
                }
                String showContent = "";
                switch (v.getId()) {
                    case R.id.imv_close:
                        popSoundWindow.dissmiss();
                        break;
                }
            }
        };
        contentView.findViewById(R.id.imv_close).setOnClickListener(listener);

        CheckBox music = contentView.findViewById(R.id.checkbox_music);
        music.setOnCheckedChangeListener(this);
        CheckBox sound = contentView.findViewById(R.id.checkbox_sound);
        sound.setOnCheckedChangeListener(this);
    }

    /**
     * 处理弹出显示内容、点击事件等逻辑
     *
     * @param contentView
     */
    private void handleClose(View contentView) {
        View.OnClickListener listener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (v.getId()) {
                    case R.id.tv_keep:
                        if (popCloseWindow != null) {
                            popCloseWindow.dissmiss();
                        }
                        break;
                    case R.id.tv_abandon:
                        BaseActivity.clearActicity();
                        startActivity(new Intent(NombreActivity.this, WelcomeActivity.class));
                        break;
                }
            }
        };
        contentView.findViewById(R.id.tv_keep).setOnClickListener(listener);
        contentView.findViewById(R.id.tv_abandon).setOnClickListener(listener);

    }


    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        switch (buttonView.getId()) {
            case R.id.checkbox_music:
                if (isChecked) {
                    buttonView.setBackgroundColor(getResources().getColor(R.color.main_sky));
                    buttonView.setText("ON");
                } else {
                    buttonView.setBackgroundColor(getResources().getColor(R.color.main_fense));
                    buttonView.setText("OFF");
                }
                break;
            case R.id.checkbox_sound:
                if (isChecked) {
                    buttonView.setBackgroundColor(getResources().getColor(R.color.main_sky));
                    buttonView.setText("ON");
                } else {
                    buttonView.setBackgroundColor(getResources().getColor(R.color.main_fense));
                    buttonView.setText("OFF");
                }
                break;
        }
    }
}

class NombreBean {
    public int mName;
    public Drawable mFront;
    public Drawable mBack;
}
